package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.Chapter;
import com.lzq.course.server.domain.ChapterExample;
import com.lzq.course.server.domain.CourseExample;
import com.lzq.course.server.dto.ChapterDto;
import com.lzq.course.server.dto.ChapterPageDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.mapper.ChapterMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ChapterService {

    @Resource
    private ChapterMapper chapterMapper;

    /**
     * example 这个文件很有用
     * 可以帮我们生成where order 查询
     * 不用单独写sql的增删改查
     */
    public void list(ChapterPageDto chapterPageDto) {
        // 分页查询 页码不是从0开始
        PageHelper.startPage(chapterPageDto.getPage(), chapterPageDto.getSize());

        // id从低到高排序
        ChapterExample chapterExample = new ChapterExample();
        chapterExample.setOrderByClause("id desc");

        // 指定 课程
        ChapterExample.Criteria chapterExampleCriteria = chapterExample.createCriteria();
        if (!StringUtils.isEmpty(chapterPageDto.getCourseId())) {
            chapterExampleCriteria.andCourseIdEqualTo(chapterPageDto.getCourseId());
        }

        // 查询
        List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);

        PageInfo<Chapter> pageInfo = new PageInfo<>(chapterList);
        chapterPageDto.setTotal(pageInfo.getTotal());

       // List<ChapterDto> chapterDtoList = new ArrayList<>();

//        for (Chapter chapter : chapterList) {
//            ChapterDto chapterDto = new ChapterDto();
//            chapterDtoList.add(chapterDto);
//            BeanUtils.copyProperties(chapter, chapterDto);
//        }

        List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);

        chapterPageDto.setList(chapterDtoList);
    }

    /**
     * 大章 保存 修改
     * @param chapterDto
     */
    public void save(ChapterDto chapterDto) {
        //BeanUtils.copyProperties(chapterDto, chapter);
        Chapter copyChapter = CopyUtil.copy(chapterDto, Chapter.class);

        if (StringUtils.isEmpty(chapterDto.getId())) {
            // 新增
            // 八位的 id
            copyChapter.setId(UuidUtil.getShortUuid());
            chapterMapper.insert(copyChapter);
        } else {
            // 编辑
            chapterMapper.updateByPrimaryKey(copyChapter);
        }
    }

    /**
     * 删除大章
     * @param id
     */
    public void delete(String id) {
        chapterMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询某一课程下的所有章
     */
    public List<ChapterDto> listByCourse(String courseId) {
        ChapterExample example = new ChapterExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        List<Chapter> chapterList = chapterMapper.selectByExample(example);
        List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);
        return chapterDtoList;
    }
}
